/**
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2022. All rights reserved.
 */

#include "vpc_pipe_comm.h"
#include "securec.h"

void vpc_pipe_set_src_pic_info(vpc_pipe_ctx_t *ctx, const vpc_pipe_pic_info_t *src)
{
    uint8_t vpu_id = ctx->vpu_id;
    vpc_pipe_private_data_t *pdata = &ctx->pdata[vpu_id];

    pdata->src_use_apart_pic = 0;
    pdata->hfbc_en = 0;
    // init src and dst roi region
    pdata->src_roi.hmin = 0;
    pdata->src_roi.hmax = src->picture_width - 1;
    pdata->src_roi.vmin = 0;
    pdata->src_roi.vmax = src->picture_height - 1;
    pdata->src_roi.width = src->picture_width;
    pdata->src_roi.height = src->picture_height;
    (void)memcpy_s(&pdata->src, sizeof(vpc_pipe_pic_info_t), src, sizeof(vpc_pipe_pic_info_t));
    // init format information used in subsequent process
    vpc_pipe_init_format_info(&pdata->src_fmt_info, src->picture_format);
}

void vpc_pipe_set_src_hfbc_info(vpc_pipe_ctx_t *ctx, vpc_hfbc_info_t *src)
{
    uint8_t vpu_id = ctx->vpu_id;
    vpc_pipe_private_data_t *pdata = &ctx->pdata[vpu_id];

    pdata->src_use_apart_pic = 0;
    pdata->hfbc_en = 1;
    // init src and dst roi region
    pdata->src_roi.hmin = 0;
    pdata->src_roi.hmax = src->width - 1;
    pdata->src_roi.vmin = 0;
    pdata->src_roi.vmax = src->height - 1;
    pdata->src_roi.width = ALIGN_UP(src->width, 64); // 宽64对齐
    pdata->src_roi.height =  ALIGN_UP(src->height, 8); // 高8对齐
    (void*)memcpy_s(&pdata->hfbc_src, sizeof(vpc_hfbc_info_t), src, sizeof(vpc_hfbc_info_t));
    // init format information used in subsequent process
    vpc_pipe_init_format_info(&pdata->src_fmt_info, src->picture_format);
}

void vpc_pipe_set_dst_pic_info(vpc_pipe_ctx_t *ctx, const vpc_pipe_pic_info_t *dst[PIPE_OUT_NUM], uint32_t num)
{
    uint32_t i;
    uint8_t vpu_id = ctx->vpu_id;
    vpc_pipe_private_data_t *pdata = &ctx->pdata[vpu_id];
    pdata->dst_num = num;
    pdata->dst_use_apart_pic = 0;
    for (i = 0; i < num; i++) {
        (void)memcpy_s(&pdata->dst[i], sizeof(vpc_pipe_pic_info_t), dst[i], sizeof(vpc_pipe_pic_info_t));
        vpc_pipe_init_format_info(&pdata->dst_fmt_info[i], dst[i]->picture_format);
    }
}

void vpc_pipe_set_apart_src_pic_info(vpc_pipe_ctx_t *ctx, vpc_apart_pic_info_t *apart_src)
{
    uint8_t vpu_id = ctx->vpu_id;
    vpc_pipe_private_data_t *pdata = &ctx->pdata[vpu_id];
    pdata->src_use_apart_pic = 1;
    pdata->hfbc_en = 0;
    pdata->src_roi.hmin = 0;
    pdata->src_roi.hmax = apart_src->width - 1;
    pdata->src_roi.vmin = 0;
    pdata->src_roi.vmax = apart_src->height - 1;
    pdata->src_roi.width = apart_src->width;
    pdata->src_roi.height = apart_src->height;
    (void*)memcpy_s(&pdata->apart_src, sizeof(vpc_apart_pic_info_t), apart_src, sizeof(vpc_apart_pic_info_t));
    vpc_pipe_init_format_info(&pdata->src_fmt_info, apart_src->picture_format);
}

void vpc_pipe_set_apart_dst_pic_info(vpc_pipe_ctx_t *ctx, vpc_apart_pic_info_t *apart_dst[PIPE_OUT_NUM], uint32_t num)
{
    uint32_t i;
    uint8_t vpu_id = ctx->vpu_id;
    vpc_pipe_private_data_t *pdata = &ctx->pdata[vpu_id];
    pdata->dst_num = num;
    pdata->dst_use_apart_pic = 1;
    for (i = 0; i < num; i++) {
        (void*)memcpy_s(&pdata->apart_dst[i], sizeof(vpc_apart_pic_info_t), apart_dst[i], sizeof(vpc_apart_pic_info_t));
        vpc_pipe_init_format_info(&pdata->dst_fmt_info[i], apart_dst[i]->picture_format);
    }
}

void vpc_pipe_set_default(const vpc_pipe_ctx_t *ctx)
{
    uint8_t i;
    uint8_t vpu_id = ctx->vpu_id;
    vpu_vpc_config_t *vpu = &ctx->vpu_config[vpu_id];

    vpu->top.bitWidth = BIT_8;
    vpu->top.rdChannelSel = CVDR_CHANNEL;
    vpu->top.errMask = 0xE0FC;   // 默认错误中断mask
    vpu->top.pipeIntMerge = 0x0; // 默认汇聚中断初始化为0
    vpu->top.multiLevelZoomEn = ONE_TO_THREE_MODE;
    vpu->top.out1To3En = 1;      // bit0为1表示只打开主通路
    vpu->pipe.top.kernelFmt = RGB888_PACKED;
    vpu->pipe.top.channelOut = AI_CHANNEL_OUT;

    for (i = 0; i < NR_RD_NUM; i++) {
        vpu->channel.cvdr.nr.rd[i].splitEn = 1;     // 使能
        vpu->channel.cvdr.nr.rd[i].timeoutTrld = 5; // 默认值值为5
        vpu->channel.cvdr.nr.rd[i].allocatedDu = 63; // 默认值值为63
        vpu->channel.cvdr.nr.rd[i].en = 1;          // 使能
    }
    for (i = 0; i < NR_WR_NUM; i++) {
        vpu->channel.cvdr.nr.wr[i].splitEn = 1;     // 使能
        vpu->channel.cvdr.nr.wr[i].timeoutTrld = 5; // 默认值值为5
    }
}
